package com.zhou.util;

import com.alibaba.fastjson.JSON;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lang.zhou
 * @date 2019/11/8
 */
public class TimeUtil {
    public static String formatDate(Date date, String format){
        SimpleDateFormat fm = new SimpleDateFormat(format);
        return fm.format(date);
    }
    public static String formatNow(String format){
        return formatDate(new Date(),format);
    }
    public static Date parseDate(String date, String format){
        SimpleDateFormat fm = new SimpleDateFormat(format);
        try {
            return fm.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException("日期转换错误");
        }
    }

    public static String formatLongDate(Long l, String format){
        Date date = new Date(l);
        SimpleDateFormat fm = new SimpleDateFormat(format);
        return fm.format(date);
    }
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }
    public static String getServerStartDate(String format) {
        return formatDate(getServerStartDate(),format);
    }

    public static void filterListDate(List<Map<String,Object>> list, String format){
        Map<String,Object> forMap = JSON.parseObject(format);
        for (Map<String, Object> map : list) {
            filterListDate(map,forMap);
        }
    }
    /**
     * 计算两个时间差
     */
    public static String getDateBetweenString(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static void filterListDate(Map<String,Object> map, Map<String,Object> forMap){
        for (String f : forMap.keySet()) {
            String fmt = StringTool.safeToString(forMap.get(f),"");
            if(fmt .length() == 0){
                fmt = DEFAULT_FORMAT;
            }
            Object o = map.get(f);
            if(o instanceof Date){
                map.put(f,formatDate((Date)o,fmt));
            }
        }

    }

    /**
     * 获得第前n天或后n天
     */
    public static Date getPreOrNextDay(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(6, n);
        return calendar.getTime();
    }

    /**
     * 加上小时
     */
    public static Date addHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }

    /**
     * 加上分钟
     */
    public static Date addMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }
    public static Date getSysDateAsDate() {
        return Calendar.getInstance().getTime();
    }
    public static String getCurrentDate(String format) {
        return formatDate(Calendar.getInstance().getTime(), format);
    }
    public static String getSysTime() {
        return getCurrentDate("HH:mm:ss");
    }
    public static int getBetweenSecond(Date start, Date end){
        long s = start.getTime();
        long e = end.getTime();

        return (int) ((e-s)/1000);
    }
    public static String toRecentTime(Date date){
        Date now = new Date();
        String r ;
        int d = DateUtil.getBetweenDay(date,now);
        if(d== 0){
            r = TimeUtil.formatDate(date,"HH:mm");
        }else if(d==1){
            r = "昨天 "+TimeUtil.formatDate(date,"HH:mm");
        }else if(d==2){
            r = "前天 "+TimeUtil.formatDate(date,"HH:mm");
        }else{
            int y1 = now.getYear();
            int y2 = date.getYear();
            if(y1==y2){
                r = TimeUtil.formatDate(date,"MM-dd");
            }else{
                r = TimeUtil.formatDate(date,"yyyy-MM-dd HH:mm");
            }

        }
        return r;
    }

    public static String toPassTime(Long duration){
        StringBuilder r = new StringBuilder();
        if(duration != null){
            int second = (int) (duration/1000);

            if(second >= 60){
                int minute = second/60;
                int secondL = second%60;
                if(minute >= 60){
                    int hour = minute/60;
                    int minuteL = minute%60;
                    if(hour >= 24){
                        int day = hour/24;
                        int hourL = hour%24;
                        r.append(day).append("天");
                        if(hourL > 0){
                            r.append(hourL).append("小时");
                        }
                    }else{
                        r.append(hour).append("小时");
                        if(minuteL > 0){
                            r.append(minuteL).append("分钟");
                        }
                    }
                }else{
                    r.append(minute).append("分钟");
                    if(secondL > 0){
                        r.append(secondL).append("秒");
                    }
                }
            }else{
                r.append(second).append("秒");
            }
        }
        return r.toString();
    }

    public static void main(String[] args) {
        System.out.println(toPassTime(1000L*60*60*24*5 + 1000L*60*60*3));
    }
}
